package com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.dto.ErpFinancereceiptDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.dto.SaveErpFinancereceiptFormDataDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.vo.ErpFinancereceiptFormDataVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.vo.ErpFinancereceiptListVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.dto.ErpFinancereceiptListDto;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.vo.ErpFinancereceiptbillVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.entity.ErpFinancereceiptbill;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.dto.ErpFinancereceiptbillDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.entity.ErpFinancereceipt;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.vo.ErpFinancereceiptVo;
import com.xjrsoft.module.customer.erp_model.eRP_FinanceReceipt.service.IErpFinancereceiptService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-09-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_financereceipt")
@Api(value = "收款管理", tags = "收款管理接口")
public class ErpFinancereceiptController {


	private final IErpFinancereceiptService erpFinancereceiptService;

	private final IXjrBaseCodeRuleService codeRuleService;

	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpFinancereceipt")
	public Response<ErpFinancereceiptFormDataVo> getFormData(@PathVariable String id) {
		ErpFinancereceiptFormDataVo formDataVo = new ErpFinancereceiptFormDataVo();
		// 主表数据
		ErpFinancereceipt erpFinancereceipt = erpFinancereceiptService.getById(id);
		formDataVo.setErpFinancereceiptVo(BeanUtil.copy(erpFinancereceipt, ErpFinancereceiptVo.class));
		// 从表数据
		formDataVo.setErpFinancereceiptbillVoList(BeanUtil.copyList(erpFinancereceiptService.getErpFinancereceiptbillByParentId(id), ErpFinancereceiptbillVo.class));
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpFinancereceipt")
	public Response<PageOutput<ErpFinancereceiptListVo>> getPageList(ErpFinancereceiptListDto listDto) {
		IPage<ErpFinancereceipt> page = erpFinancereceiptService.getPageList(listDto);
		List<ErpFinancereceiptListVo> records = BeanUtil.copyList(page.getRecords(), ErpFinancereceiptListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpFinancereceipt")
	public Response save(@RequestBody SaveErpFinancereceiptFormDataDto formDto) {
		ErpFinancereceiptDto erpFinancereceiptDto = formDto.getErpFinancereceiptDto();
		ErpFinancereceipt erpFinancereceipt = BeanUtil.copy(erpFinancereceiptDto, ErpFinancereceipt.class);
		List<ErpFinancereceiptbillDto> erpFinancereceiptbillDtoList = formDto.getErpFinancereceiptbillDto();
		List<ErpFinancereceiptbill> erpFinancereceiptbillList = BeanUtil.copyList(erpFinancereceiptbillDtoList, ErpFinancereceiptbill.class);
		String dataId = erpFinancereceiptService.addErpFinancereceipt(erpFinancereceipt, erpFinancereceiptbillList);
		if (dataId != null) {
			codeRuleService.useEncode("skbm");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpFinancereceipt")
	public Response update(@PathVariable String id, @RequestBody SaveErpFinancereceiptFormDataDto formDto) {
		ErpFinancereceiptDto erpFinancereceiptDto = formDto.getErpFinancereceiptDto();
		ErpFinancereceipt erpFinancereceipt = BeanUtil.copy(erpFinancereceiptDto, ErpFinancereceipt.class);

		List<ErpFinancereceiptbillDto> erpFinancereceiptbillDtoList = formDto.getErpFinancereceiptbillDto();
		List<ErpFinancereceiptbill> erpFinancereceiptbillList = BeanUtil.copyList(erpFinancereceiptbillDtoList, ErpFinancereceiptbill.class);
		return Response.status(erpFinancereceiptService.updateErpFinancereceipt(id, erpFinancereceipt, erpFinancereceiptbillList));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpFinancereceiptService.removeByIds(Func.toStrList(ids)));
	}

	
}
